<?php
/**

 */
defined('IN_IA') or exit('Access Denied');


function url($segment, $params = array()) {
	return wurl($segment, $params);
}


function message($msg, $redirect = '', $type = '') {
	global $_W;
	if($redirect == 'refresh') {
		$redirect = $_W['script_name'] . '?' . $_SERVER['QUERY_STRING'];
	}
	if($redirect == 'referer') {
		$redirect = referer();
	}
	if($redirect == '') {
		$type = in_array($type, array('success', 'error', 'info', 'warning', 'ajax', 'sql')) ? $type : 'info';
	} else {
		$type = in_array($type, array('success', 'error', 'info', 'warning', 'ajax', 'sql')) ? $type : 'success';
	}
	if($_W['isajax'] || $type == 'ajax') {
		$vars = array();
		$vars['message'] = $msg;
		$vars['redirect'] = $redirect;
		$vars['type'] = $type;
		exit(json_encode($vars));
	}
	if (empty($msg) && !empty($redirect)) {
		header('location: '.$redirect);
	}
	$label = $type;
	if($type == 'error') {
		$label = 'danger';
	}
	if($type == 'ajax' || $type == 'sql') {
		$label = 'warning';
	}
	include template('common/message', TEMPLATE_INCLUDEPATH);
	exit();
}


function checklogin() {
	global $_W;
	if (empty($_W['uid'])) {
		message('抱歉，您无权进行该操作，请先登录！', url('user/login'), 'warning');
	}
	return true;
}


function checkaccount() {


}

function buildframes($types = array('platform')) {
	checkaccount();
	$ms = include IA_ROOT . '/web/common/frames.inc.php';
	$ms = array_elements($types, $ms);
	
	if(in_array('ext', $types)) {
		load()->model('module');
		$modules = uni_modules();
		if(!empty($modules)) {
			foreach($modules as $m) {
				$items = array();
				if($m['isrulefields']) {
					$items[] = array(
						'关键字触发列表',
					    url('rule/display', array('module' => $m['name'])),
						'childItems' => array('<i class="icon-plus"></i>', url('rule/post', array('module' => $m['name']))),
					);
				}
				$entries = module_entries($m['name'], array('menu'));
				if(!empty($entries)) {
					$ms['ext'][] = array(
						'title' => $m['title'],
						'items' => $entries['menu']
					);
				}
			}
		}
	}
	return $ms;
}
